﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Types;

namespace Client
{
    public partial class AddAnswerForm : Form
    {
        private string username { get; set; }
        private QuestionType question { get; set; }
        private List<AnswerType> answerList = new List<AnswerType>();

        public delegate void UpdateAnswerListDelegate(List<AnswerType> answerList);

        public AddAnswerForm(QuestionType question, string username)
        {
            InitializeComponent();
            this.username = username;
            this.question = question;
            ClientManager.Instance.updateAnswerList += new ClientManager.UpdateAnswerList(this.UpdateAnswerList);
        }

        private void UpdateAnswerList(List<AnswerType> answerList)
        {
            if (InvokeRequired)
            {
                Invoke(new UpdateAnswerListDelegate(this.UpdateAnswerList), answerList);
            }
            else
            {
                questionTextBox.Text = question.summary;
                answersListBox.Items.Clear();
                foreach (var answer in answerList)
                {
                    answersListBox.Items.Add(answer.summary);
                }
                this.answerList = answerList;
            }
        }

        private void submitButton_Click(object sender, EventArgs e)
        {
            ClientManager.Instance.AddNewAnswer(0, addAnswerTextBox.Text, question.id, "");
            this.Close();
        }
    }
}
